package com.kj.report.mongoRepository;

import com.kj.report.model.ReportBaseThreeData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ReportBaseThreeDataRepository {
    @Autowired
    private MongoTemplate mongoTemplate;

    public List<ReportBaseThreeData> findAll() {

        return mongoTemplate.find(new Query(), ReportBaseThreeData.class);
    }

    /**
     * 插入
     *
     * @param ReportBaseThreeData
     */
    public void insert(ReportBaseThreeData ReportBaseThreeData) {
        mongoTemplate.insert(ReportBaseThreeData);
    }
    /**
     * 插入
     *
     * @param ReportBaseThreeData
     */
    public void insert(List<ReportBaseThreeData> ReportBaseThreeData) {
        mongoTemplate.insert(ReportBaseThreeData);
    }

    /**
     * 根据userId 查询
     * @param userId
     * @return
     */
    public List<ReportBaseThreeData> findByUserId(Integer userId) {
        Criteria criteria = new Criteria("userId").is(userId);
        return mongoTemplate.find(new Query(criteria), ReportBaseThreeData.class);

    }
    /**
     * 根据报告ID 查询
     * @param inspectId
     * @return
     */
    public ReportBaseThreeData findById(String inspectId) {
        return mongoTemplate.findOne(new Query(Criteria.where("_id").is(inspectId)), ReportBaseThreeData.class);
    }

    /**
     * 删除所有
     */
    public void removeAll() {
        List<ReportBaseThreeData> list = this.findAll();
        if (list != null) {
            for (ReportBaseThreeData reportView : list) {
                mongoTemplate.remove(reportView);
            }
        }

    }

    /**
     * 根据ID删除报告
     * @param id
     */
    public void removeOne(String id) {
        Query query = Query.query(Criteria.where("_id").is(id));
        mongoTemplate.remove(query, ReportBaseThreeData.class);
    }

    /**
     * 根据报告id修改bmi值
     * @auther: cwh
     * @date: 2018/10/12 14:26
     * @param: [id, bmiValue]
     * @return: void
     */
    public void updateBmiById(String inspectId,double bmiValue){
        Query query = Query.query(Criteria.where("_id").is(inspectId));
        Update update = Update.update("reportNewThreeViems.3.targetName", "BMI").set("reportNewThreeViems.3.score", bmiValue);
        mongoTemplate.updateFirst(query,update,ReportBaseThreeData.class);
    }
}
